package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TbUnits;
import com.ruoyi.basice_info.domain.TsExamSyllabusVo;
import com.ruoyi.basice_info.mapper.TbUnitsMapper;
import com.ruoyi.basice_info.mapper.TsExamSyllabusVoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbSubjectMapper;
import com.ruoyi.basice_info.domain.TbSubject;
import com.ruoyi.basice_info.service.ITbSubjectService;

/**
 * 科目设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-07
 */
@Service
public class TbSubjectServiceImpl implements ITbSubjectService
{
    @Autowired
    private TbSubjectMapper tbSubjectMapper;


    @Autowired
    private TbUnitsMapper tbUnitsMapper;

    @Autowired
    private TsExamSyllabusVoMapper tsExamSyllabusVoMapper;

    /**
     * 查询科目设置
     *
     * @param subjectId 科目设置主键
     * @return 科目设置
     */
    @Override
    public TbSubject selectTbSubjectBySubjectId(Long subjectId)
    {
        return tbSubjectMapper.selectTbSubjectBySubjectId(subjectId);
    }

    @Override
    public TbSubject getTbSubjectBySubjectCode(String code) {
        return tbSubjectMapper.getTbSubjectBySubjectCode(code);
    }

    @Override
    public TbSubject getTbSubjectBySubjectName(String name) {
        return tbSubjectMapper.getTbSubjectBySubjectName(name);
    }

    /**
     * 查询科目设置列表
     *
     * @param tbSubject 科目设置
     * @return 科目设置
     */
    @Override
    public List<TbSubject> selectTbSubjectList(TbSubject tbSubject)
    {

        return tbSubjectMapper.selectTbSubjectList(tbSubject);
    }

    /**
     * 新增科目设置
     *
     * @param tbSubject 科目设置
     * @return 结果
     */
    @Override
    public int insertTbSubject(TbSubject tbSubject)
    {
        return tbSubjectMapper.insertTbSubject(tbSubject);
    }

    /**
     * 修改科目设置
     *
     * @param tbSubject 科目设置
     * @return 结果
     */
    @Override
    public int updateTbSubject(TbSubject tbSubject)
    {
        return tbSubjectMapper.updateTbSubject(tbSubject);
    }

    /**
     * 批量删除科目设置
     *
     * @param subjectIds 需要删除的科目设置主键
     * @return 结果
     */
    @Override
    public int deleteTbSubjectBySubjectIds(Long[] subjectIds)
    {
        int num = -1;
        int nums = 0;

        List<Long> longList = Arrays.asList(subjectIds);
        for (Long item:longList) {

            TsExamSyllabusVo syllabusVo = new TsExamSyllabusVo();
            syllabusVo.setSubjectId(item);
            List<TsExamSyllabusVo> voList = tsExamSyllabusVoMapper.selectTsExamSyllabusList(syllabusVo);
            TbUnits tbUnits = new TbUnits();
            tbUnits.setSubjectId(item);
            List<TbUnits> list = tbUnitsMapper.selectTbUnitsList(tbUnits);
            //  去查 如果没有就删除
            if (list.size() <= 0 || voList.size()<=0) { // 这样要使用或
                // 如果不存在那么就删除
                num = tbSubjectMapper.deleteTbSubjectBySubjectId(item);
            } else {
                nums += 1;
            }
        }
        if(longList.size() == 1) {
            if (nums>0) {
                throw new IllegalArgumentException("该数据在单元设置或者考试详情中使用，不能删除");
            } else {
                if (nums > 0) {
                    throw new IllegalArgumentException(nums + " 条数据删除失败，数据在单元设置或者考试详情中使用，不能删除");
                }
            }
        }
//        return tbSubjectMapper.deleteTbSubjectBySubjectIds(subjectIds);
        return num;
    }

    /**
     * 删除科目设置信息
     *
     * @param subjectId 科目设置主键
     * @return 结果
     */
    @Override
    public int deleteTbSubjectBySubjectId(Long subjectId)
    {
        return tbSubjectMapper.deleteTbSubjectBySubjectId(subjectId);
    }
}
